package com.jiezhuo.controller.jw;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiezhuo.entity.LoginUser;
import com.jiezhuo.entity.jw.*;
import com.jiezhuo.service.jw.JwUserlistService;
import com.jiezhuo.service.jw.JwXqBjkcbzrService;
import com.jiezhuo.service.jw.JwXqJsrkbService;
import com.jiezhuo.service.jw.JwXqKskcService;
import com.jiezhuo.utils.R;
import com.sun.xml.bind.v2.TODO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.SerializationUtils;
import org.apache.ibatis.annotations.Param;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.jiezhuo
 * @since 2024-06-15
 */
@RestController
@RequestMapping("/jw-xq-kskc")
@Api(tags = "学科设置")
public class JwXqKskcController {

    @Autowired
    JwUserlistService jwUserlistService;
    @Autowired
    JwXqJsrkbService jwXqJsrkbService;

    @Autowired
    JwXqKskcService jwXqKskcService;
    @Autowired
    JwXqBjkcbzrService jwXqBjkcbzrService;
    @ApiOperation("获得未设置学科")
    @GetMapping("getIsNotSetSubject")
    public R getIsNotSetSubject(JwVo jwVo){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        jwVo.setUserId(userId);
        R userPermissionInfo = jwUserlistService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(jwVo.getGzXqdm())){
            jwVo.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jwVo.getGzXqdm() + "'");
            if (!contains){
                jwVo.setGzXqdm("'not'");
            }
        }
        return jwXqKskcService.getIsNotSetSubject(jwVo);
    }

    @ApiOperation("添加学科")
    @PostMapping("saveSubject")
    public R saveSubject(@RequestBody JwXqKskc jwXqKskc){
//        TODO  判断学科是否存在

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        System.out.println(jwXqKskc);
        jwXqKskc.setSyzt("1");
        jwXqKskc.setXh(0);
        jwXqKskc.setOperator(userId);
        jwXqKskc.setOperateTime(new Date());
        int insert=0;
        List<String> kcIds = jwXqKskc.getKcIds();
        ArrayList<JwXqKskc> jwXqKskcs = new ArrayList<>();

        for (String kcId : kcIds) {
            JwXqKskc clone = SerializationUtils.clone(jwXqKskc);
            clone.setKcId(kcId);
            jwXqKskcs.add(clone);
        }
        boolean b = jwXqKskcService.saveBatch(jwXqKskcs);
        return b ?R.ok(): R.err();
    }



    @ApiOperation("删除学科")
    @PostMapping("delSubject")
    public R delSubject(@RequestBody JwXqKskc jwXqKskc){
        QueryWrapper<JwXqKskc> jwXqKskcQueryWrapper = new QueryWrapper<>();
        int insert=0;
        if(!jwXqKskc.getIds().isEmpty()){
            List<String> ids = jwXqKskc.getIds();
            jwXqKskcQueryWrapper.in("ID",ids);
            insert = jwXqKskcService.getBaseMapper().delete(jwXqKskcQueryWrapper);
        }else {
             insert = jwXqKskcService.getBaseMapper().deleteById(jwXqKskc);
        }

        return insert>0? R.ok(): R.err();
    }

    @ApiOperation("获取学校学科教师")
    @GetMapping("selectTeacherSubject")
    public R selectTeacherSubject( JwVo jwVo){
//        TODO  判断学科是否存在

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        jwVo.setUserId(userId);
        R userPermissionInfo = jwUserlistService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(jwVo.getGzXqdm())){
            jwVo.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jwVo.getGzXqdm() + "'");
            if (!contains){
                jwVo.setGzXqdm("'not'");
            }
        }

        R r= jwXqKskcService.selectTeacherSubject(jwVo);
        return r;
    }





    @ApiOperation("新增任课信息")
    @PostMapping("saveTeacherSubject")
    public R saveTeacherSubject(@RequestBody JwXqJsrkb jwXqBjkcbzr){
        QueryWrapper<JwXqJsrkb> jwXqKskcQueryWrapper = new QueryWrapper<>();
        jwXqKskcQueryWrapper.eq("NEW_XQDM",jwXqBjkcbzr.getNewXqdm());
        jwXqKskcQueryWrapper.eq("XQ",jwXqBjkcbzr.getXq());
        jwXqKskcQueryWrapper.eq("KC_ID",jwXqBjkcbzr.getKcId());
        jwXqKskcQueryWrapper.eq("KCXZ",jwXqBjkcbzr.getKcxz());
        jwXqKskcQueryWrapper.eq("JXZZFS",jwXqBjkcbzr.getJxzzfs());
        jwXqKskcQueryWrapper.eq(!ObjectUtils.isEmpty(jwXqBjkcbzr.getBjId()),"BJ_ID",jwXqBjkcbzr.getBjId());
        jwXqKskcQueryWrapper.isNotNull("RJZ");
        JwXqJsrkb jwXqJsrkb = jwXqJsrkbService.getBaseMapper().selectOne(jwXqKskcQueryWrapper);
        if(!ObjectUtils.isEmpty(jwXqJsrkb)){
            return R.err().msg("班级已存在此科目的任课教师");
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        jwXqBjkcbzr.setOperator(userId);
        jwXqBjkcbzr.setOperateTime(new Date());
        int insert = jwXqJsrkbService.getBaseMapper().insert(jwXqBjkcbzr);
        return insert>0? R.ok(): R.err();
    }
    @ApiOperation("删除任课信息")
    @PostMapping("delTeacherSubject")
    public R delTeacherSubject(@RequestBody JwXqJsrkb jwXqKskc){
        int i = jwXqJsrkbService.getBaseMapper().deleteById(jwXqKskc);
        return i>0? R.ok(): R.err();
    }

}

